package com.fantasy.getbom;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import com.fantasy.excel.ReadExcel;
import com.fantasy.excel.WriteExcel;
import com.fantasy.file.GetFolderAbsPath;

public class GetAllBom {

	private static List<ArrayList<String>> dataList = new ArrayList<ArrayList<String>>();
	private static String startFolder = "";
	
	public static void eachFile(String folderName){
		File xlsFolder = new File(GetFolderAbsPath.getFolderPath(folderName));
		if(xlsFolder.exists()){
			//System.out.println(xlsFolder.listFiles().length);
			for (File xlsFile : xlsFolder.listFiles()) {
				if(xlsFile.isDirectory()){
					System.out.println("folder name is " + xlsFile.getName());
					String xlsFolderName = xlsFile.getAbsolutePath();
					System.out.println("start folder is :" + startFolder);
					System.out.println("next folder is :" + xlsFolderName.replace(startFolder, ""));
					eachFile(xlsFolderName.replace(startFolder, ""));
				}else{
					getDataSheet(xlsFile);
				}
			}
		}else{
			System.out.println("folder not found " + xlsFolder);
		}
	}
	
	public static void getDataSheet(File xlsFile){
		Workbook wk = ReadExcel.getWorkbook(xlsFile);
		//System.out.println("几个sheet" + wk.getNumberOfSheets());
		for (Sheet sheet : wk) {
			if(!"Change list".equals(sheet.getSheetName())){
				ArrayList<String> data = new ArrayList<String>();
				data.add(xlsFile.getAbsolutePath());
				data.add(sheet.getSheetName());
				data.add("");
				dataList.add(data);
				getData(sheet);
			}
			
		}
	}
	
	public static void getData(Sheet sheet){
		for (Row row : sheet) {
			//System.out.println("row num : " + sheet.getLastRowNum());
			ArrayList<String> data = new ArrayList<String>();
			String num1 = ReadExcel.getCellFormatValue(row.getCell(0));
			String num = ReadExcel.getCellFormatValue(row.getCell(1));
			String type = ReadExcel.getCellFormatValue(row.getCell(2));
			String desc = ReadExcel.getCellFormatValue(row.getCell(3));
			String desc1 = ReadExcel.getCellFormatValue(row.getCell(4));
			//System.out.println(num);
			data.add(num1);
			data.add(num);
			data.add(type);
			data.add(desc);
			data.add(desc1);
			if(num.equals("") && type.equals("") && desc.equals("")){
			}else{
				dataList.add(data);
			}
		}
	}
	
	public static void main(String[] args) {
		//WriteTxt.open(GetFolderAbsPath.getPath() + "/siPartList.txt");
		startFolder = GetFolderAbsPath.getPath();
		eachFile("BOM");
		WriteExcel.writeToExcel(dataList,GetFolderAbsPath.getPath() + "bom");
	}
}
